/* HTTP Restful API Server Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_vfs_semihost.h"
#include "esp_vfs_fat.h"
#include "esp_spiffs.h"
#include "sdmmc_cmd.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "esp_log.h"
#include "mdns.h"
#include "lwip/apps/netbiosns.h"
#include "protocol_examples_common.h"
#if CONFIG_EXAMPLE_WEB_DEPLOY_SD
#include "driver/sdmmc_host.h"
#endif

#define MDNS_HOST_NAME "esp_toolbox"
#define MDNS_INSTANCE "esp wireless toolbox"
#define MOUNT_FS_PATH "/www"

static const char *TAG = "main";

static void initialise_mdns(void)
{
    mdns_init();
    mdns_hostname_set(MDNS_HOST_NAME);
    mdns_instance_name_set(MDNS_INSTANCE);

    mdns_txt_item_t serviceTxtData[] = {
#ifdef CONFIG_IDF_TARGET_ESP32
        {"board", "esp32"},
#elif defined(CONFIG_IDF_TARGET_ESP32C2)
        {"board", "esp32-c2"},
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
        {"board", "esp32-c3"},
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
        {"board", "esp32-s2"},
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
        {"board", "esp32-s3"},
#endif
        {"path", "/"}};

    ESP_ERROR_CHECK(mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData,
                                     sizeof(serviceTxtData) / sizeof(serviceTxtData[0])));
}

esp_err_t init_fs(void)
{
    wl_handle_t wl_handle;
    const esp_vfs_fat_mount_config_t mount_config = {
        .format_if_mount_failed = false,
#ifdef CONFIG_SPIRAM
        .max_files = 10,
#else
        .max_files = 3,
#endif
        .allocation_unit_size = CONFIG_WL_SECTOR_SIZE * 2};
    esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_FS_PATH, NULL, &mount_config, &wl_handle);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to mount or format filesystem");
        return err;
    }
    uint64_t total = 0, used = 0;
    err = esp_vfs_fat_info(MOUNT_FS_PATH, &total, &used);
    if (err != ESP_OK)
        ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(err));
    else
        ESP_LOGI(TAG, "Partition size: total: %ld, used: %ld", (uint32_t)total, (uint32_t)used);
    return ESP_OK;
}

#include "config.h"
#include "wifi.h"
#include "api_map.h"
#include "file_tool.h"

void app_main(void)
{
    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    initialise_mdns();
    netbiosns_init();
    netbiosns_set_name(MDNS_HOST_NAME);
    ESP_ERROR_CHECK(init_fs());
    file_tool_add_mount(MOUNT_FS_PATH);
    printf("MD5: %s\r\n", WEB_INDEX_FILE_MD5);

    api_auto_init();
    api_list();
    list_files("/", 2);

    config_init("/www/config.d");
    wifi_init();
}
